package com.qingzhou.web.repository.dao;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.mybatisflex.core.query.QueryWrapper;
import com.qingzhou.common.db.dao.AbstractSqlRepository;
import com.qingzhou.web.repository.entity.PatientEntity;
import com.qingzhou.web.repository.mapper.PatientMapper;
import org.noear.solon.annotation.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Component
public class PatientDao extends AbstractSqlRepository<PatientMapper, PatientEntity> {
    public Map<String, PatientEntity> selectMapByPatientCodes(List<String> patientCodes) {
        QueryWrapper query = new QueryWrapper();
        query.in("patient_code",patientCodes);
        List<PatientEntity> entityList = this.getMapper().selectListByQuery(query);
        if(CollUtil.isNotEmpty(entityList)){
            //按entity的patientCode组装成map对象
            return entityList.stream().collect(Collectors.toMap(PatientEntity::getPatientCode, item -> item));
        }
        return new HashMap<>();
    }

    public PatientEntity selectOneByPatientCode(String patientCode) {
        if(StrUtil.isEmpty(patientCode)) return null;
        QueryWrapper query = new QueryWrapper();
        query.eq("patient_code",patientCode);
        return this.getMapper().selectOneByQuery(query);
    }
}
